//
//  JCCreditManageViewController.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/3/29.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCCreditManageViewController.h"
#import "JCCreditManageCell.h"
#import "CreditManageModel.h"
#import "JCHDBinDingViewController.h"
#import "JCAddBankCardController.h"
#import "JCPlanningModelController.h"

@interface JCCreditManageViewController ()<UITableViewDelegate,UITableViewDataSource,planAddDelegate>

@property (nonatomic, strong) UITableView * manageTableView;
@property (nonatomic, strong) NSMutableArray * arrCard;
@end

@implementation JCCreditManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"信用卡管理";
    
    [self initBarItem];
    self.arrCard = [NSMutableArray array];
    [self getCreditCardDataListIsRefresh:YES];
    [self refresh];
    [self.view addSubview:self.manageTableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrCard.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JCCreditManageCell * cell = [JCCreditManageCell tableViewCellWithTableView:tableView];
    cell.planBtn.tag = 100 + indexPath.row;
    [cell layoutIfNeeded];

    cell.creditModel = self.arrCard[indexPath.row];
    
    cell.delegate = self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];
    return headerView;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        CreditManageModel * model = self.arrCard[indexPath.row];
        
        [self DoRequsetUnbind_card:model.cardNo];
        
    }
}

// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

#pragma mark - 懒加载
- (UITableView *)manageTableView{
    if (!_manageTableView) {
        
        _manageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _manageTableView.delegate = self;
        _manageTableView.dataSource = self;
        _manageTableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];
        _manageTableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
        _manageTableView.estimatedRowHeight = 100;
    }
    
    return _manageTableView;
}

- (void)initBarItem{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addCreditCardAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    
}

// 添加信用卡的点击事件
- (void)addCreditCardAction{
    [self DoRequsetH_bind_card];
}

#pragma mark - planAddDelegate
// 点击创建还款计划
- (void)planAddAction:(UIButton *)btn{
    
    JCPlanningModelController * VC = [[JCPlanningModelController alloc] init];
    VC.model = self.arrCard[btn.tag-100];
    [self.navigationController pushViewController:VC animated:YES];
}



// 网络请求
- (void)getCreditCardDataListIsRefresh:(BOOL)refresh{
    
    [PPNetworkHelper GET:H_bank_list parameters:nil cache:NO encrypt:NO decode:NO responseCache:^(id responseCache) {
        
    } success:^(id result) {
        if ([result[@"success"] boolValue] == YES) {
            [self.arrCard removeAllObjects];
            for (NSDictionary * dic in result[@"data"]) {
                
               [self.arrCard addObject:[CreditManageModel mj_objectWithKeyValues:dic]];
            }
            NSLog(@"self.arrCard : %@",self.arrCard);
            
        }else{
            [self.view  makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        [self.manageTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}


- (void)DoRequsetH_bind_card{
    [PPNetworkHelper GET:H_bind_card parameters:@{@"type":@"1"} cache:NO encrypt:NO decode:NO responseCache:^(id responseCache) {
        
    } success:^(id result) {
        if ([result[@"success"] boolValue] == YES) {
 
            JCHDBinDingViewController *bindingVC = [[JCHDBinDingViewController alloc] init];
            bindingVC.url = result[@"data"][@"url"];
            [self.navigationController pushViewController:bindingVC animated:YES];
        }else{
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        
    } failure:^(NSError *error) {
        
    }];
}


// 解除绑卡
- (void)DoRequsetUnbind_card:(NSString *)code{
    
    [PPNetworkHelper POST:Unbind_card parameters:@{@"card_code":code} cache:NO encrypt:YES decode:NO responseCache:^(id responseCache) {
        
    } success:^(id result) {
        if ([result[@"success"] boolValue] == YES) {
            [self getCreditCardDataListIsRefresh:YES];
        }else{
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 列表刷新 加载更多

- (void)refresh{
    
    @weakify(self)
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.manageTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self getCreditCardDataListIsRefresh:YES];
        [self.manageTableView.mj_header endRefreshing];
        // 马上进入刷新状态
    }];
    
}

- (void)refreshCard{
    
    [self getCreditCardDataListIsRefresh:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
